package com.jiaquyun.jcyx.main.mine

import android.graphics.Typeface
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ConvertUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.entity.CouponList
import com.jiaquyun.jcyx.jetpack.mode.MineViewModel
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg

import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_mine_coupon.*


class MineCouponFragment : OwnerViewModeFragment<MineViewModel>() {

    companion object {
        const val KEY_COLLECT_ACTION = "KEY_COLLECT_ACTION"
        fun newInstance(action: Int): MineCouponFragment {
            val bundle = Bundle().apply {
                putInt(KEY_COLLECT_ACTION, action)
            }
            return MineCouponFragment().apply {
                arguments = bundle
            }
        }
    }

    private val mCouponListAdapter =
        object : BaseQuickAdapter<CouponList, BaseViewHolder>(R.layout.item_coupon_list) {
            override fun convert(holder: BaseViewHolder, item: CouponList) {
                holder.setText(R.id.tvCouponListMoney, "￥${item.money}")
                holder.setText(R.id.tvCouponListMoneySub, "满${item.condition}元可用")
                holder.setText(R.id.tvCouponListName, item.name)
                holder.setText(R.id.tvCouponListTime, "${item.end_time}到期")
                holder.getView<ImageView>(R.id.ivCouponListBg).apply {
                    when (item.status == 0) {
                        true -> setImageResource(R.mipmap.ic_16575)
                        else -> setImageResource(R.mipmap.ic_16576)
                    }
                }
                holder.getView<ImageView>(R.id.ivCouponListStatus).apply {
                    when (item.status) {
                        1 -> setImageResource(R.mipmap.ic_48535)
                        2 -> setImageResource(R.mipmap.ic_16298)
                        else -> setImageResource(0)
                    }
                }
            }
        }

    private val mAction: Int by lazy {
        arguments?.getInt(KEY_COLLECT_ACTION) ?: 0
    }


    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_mine_coupon

    override fun initView() {

        rvMineCouponBody.adapter = mCouponListAdapter

        mSmartRefreshLayout.setEnableRefresh(false)
        mSmartRefreshLayout.setEnableLoadMore(false)
        mSmartRefreshLayout.setOnLoadMoreListener {
            viewModel.userCouponList(mAction, isLoadMore = true)
        }
    }

    override fun initData() {
        viewModel.userCouponList(mAction)
    }

    override fun initViewMode() {

        viewModel.userCouponListLiveDate.observe(this as LifecycleOwner, Observer { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg ?: "")
                mSmartRefreshLayout.finishLoadMore((false))
            } else if (response.status == Status.SUCCESS) {
                mSmartRefreshLayout.finishLoadMore()
            }
            if (response.status != Status.SUCCESS)
                return@Observer
            response.data?.let {
                if (it.data.size == 0) {
                    mCouponListAdapter.setEmptyView(createEmptyView())
                }
                if (it.loadMore) {
                    mCouponListAdapter.addData(it.data)
                } else {
                    mCouponListAdapter.setNewInstance(it.data)
                }
                if (it.data.size < viewModel.rows) {
                    mCouponListAdapter.setFooterView(createFooterView())
                    mSmartRefreshLayout.setEnableLoadMore(false)
                } else {
                    mCouponListAdapter.removeAllFooterView()
                    mSmartRefreshLayout.setEnableLoadMore(true)
                }
            }
        })
    }


    private fun createEmptyView(): View {
        return layoutInflater.inflate(R.layout.view_empty_error, null)
    }

    private fun createFooterView(): View = TextView(requireContext()).apply {
        layoutParams = FrameLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            ConvertUtils.dp2px(30f)
        )
        gravity = Gravity.CENTER
        textSize = 13f
        text = "没有更多数据"
        setTypeface(null, Typeface.BOLD)
        setTextColor(ColorUtils.getColor(R.color.FF_666666))
    }

}